package mychnxi.demo.java.util.time;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.TemporalUnit;

@RunWith(PowerMockRunner.class)
public class PeriodDemo {

    @Before
    public void before(){
        System.out.println(">>>>>>>>>>");
    }

    /**
     * 返回两个时间点的时间间隔
     */
    @Test
    public void between_test(){
        System.out.println("=====between=====");
        String outPattern = "getYears=%d; getMonth=%d; getDays=%d;";
        // 开始时间
        LocalDate startDate = LocalDate.of(2019, 8, 12);
        // 结束时间
        LocalDate endDate = LocalDate.of(2020, 7, 13);
        // 通过between获取Period
        Period period = Period.between(startDate, endDate);

        System.out.println(String.format(outPattern, period.getYears(), period.getMonths(), period.getDays()));
    }

    /**
     * 判断开始时间是否大于结束时间
     */
    @Test
    public void isNegative_test(){
        System.out.println("=====isNegative=====");
        // 开始时间
        LocalDate startDate = LocalDate.of(2019, 8, 12);
        // 结束时间
        LocalDate endDate = LocalDate.of(2020, 7, 13);
        // 通过between获取Period
        Period period = Period.between(startDate, endDate);
        System.out.println(startDate.toString() + " : " + endDate.toString() + " isNegative =" + period.isNegative());

        period = Period.between(endDate, startDate);
        System.out.println(endDate.toString() + " : " + startDate.toString() + " isNegative =" + period.isNegative());
    }

    @Test
    public void ofDays_test(){
        System.out.println("=====ofDays=====");
        Period period = Period.ofDays(50);
        String outPattern = "getYears=%d; getMonth=%d; getDays=%d;";
        System.out.println(String.format(outPattern, period.getYears(), period.getMonths(), period.getDays()));
    }

    public void ofMonths_test(){
        System.out.println("=====ofMonths=====");
        Period period = Period.ofMonths(2);
        String outPattern = "getYears=%d; getMonth=%d; getDays=%d;";
        System.out.println(String.format(outPattern, period.getYears(), period.getMonths(), period.getDays()));
    }

    @Test
    public void ofYears_test(){
        System.out.println("=====ofYears=====");
        Period period = Period.ofYears(3);
        String outPattern = "getYears=%d; getMonth=%d; getDays=%d;";
        System.out.println(String.format(outPattern, period.getYears(), period.getMonths(), period.getDays()));
    }

    @Test
    public void ofWeeks_test(){
        System.out.println("=====ofWeeks=====");
        Period period = Period.ofWeeks(2);
        String outPattern = "getYears=%d; getMonth=%d; getDays=%d;";
        System.out.println(String.format(outPattern, period.getYears(), period.getMonths(), period.getDays()));
    }

    @Test
    public void of_test(){
        System.out.println("=====of=====");
        Period period = Period.of(1, 2, 3);
        String outPattern = "getYears=%d; getMonth=%d; getDays=%d;";
        System.out.println(String.format(outPattern, period.getYears(), period.getMonths(), period.getDays()));
    }

}
